import axios from 'axios';
import sign from '../utils/sign.js';
import cache from '../utils/cache.js';
import { Loading } from 'element-ui';


const Req = {
    get: function (url, page, cb, data, toast,target) {
        var $this = this;
        let loading=$this.getLoading(target);
        axios.request({
            url: url,
            method: 'get',
            data: data,
            headers:$this.headerHandle()
        }).then(function (res) {
            $this.resHandle(res,page,cb,toast,loading);
        }).catch(function (error) {
            $this.errorHandle(error,page,toast,loading)
        })
    },
    pget: function (url, page, cb, data, toast,target) {
        var $this = this;
        let loading=$this.getLoading(target);
        axios.request({
            url: url,
            method: 'get',
            params: data,
            headers:$this.headerHandle()
        }).then(function (res) {
            $this.resHandle(res,page,cb,toast,loading);
        }).catch(function (error) {
            $this.errorHandle(error,page,toast,loading)
        })
    },
    post: function (url, page,cb, data, toast, target) {
        var $this = this;
        let loading=$this.getLoading(target);
        axios.request({
            url: url,
            method: 'post',
            data: data,
            headers:$this.headerHandle()
        }).then(function (res) {
            console.log(res)
            $this.resHandle(res,page,cb,toast,loading);
        }).catch(function (error) {
            $this.errorHandle(error,page,toast,loading)
        })
    },
    ppost: function (url, page,cb, data, toast, target) {
        var $this = this;
        let loading=$this.getLoading(target);
        axios.request({
            url: url,
            method: 'post',
            params: data,
            headers:$this.headerHandle()
        }).then(function (res) {
            $this.resHandle(res,page,cb,toast,loading);
        }).catch(function (error) {
            $this.errorHandle(error,page,toast,loading)
        })
    },
    put: function (url,page, cb, data, toast,target) {
         var $this = this;
        let loading=$this.getLoading(target);

        axios.request({
            url: url,
            method: 'put',
            data: data,
            headers:$this.headerHandle()
        }).then(function (res) {
            $this.resHandle(res,page,cb,toast,loading);
        }).catch(function (error) {
            $this.errorHandle(error,page,toast,loading)
        })
    },
    del: function (url,page, cb, data, toast,target) {

        var $this = this;
        let loading=$this.getLoading(target);
        axios.request({
            url: url,
            method: 'delete',
            data: data,
            headers:$this.headerHandle()
        }).then(function (res) {
            $this.resHandle(res,page,cb,toast,loading);
        }).catch(function (error) {
            $this.errorHandle(error,page,toast,loading)
        })
    },

    resHandle:function(res,page,cb,toast,loading){
            loading && loading.close();
            if (res.data.error) {
                toast && page.$notify.error("操作失败,请联系管理员");
            } else {
                toast && page.$notify.success(res.data.msg);
                typeof cb === "function" && cb(res);
            }

    },
    errorHandle:function(error,page,toast,loading){
        console.log(error);
        //重新登录
        page.$notify.error("网络请求出错,请重新登录");
        loading && loading.close();



    },
    signHandle:function(params){
        params.sign = sign.getSign(params);
        return params;
    },
    headerHandle:function(){
        if(cache.cache.getToken()){
            return {Authorization:cache.cache.getToken()}
        }
    },
    preHandle:function(target){

    },
    getLoading:function(target){
        return  Loading.service({target:target?target:document.body,fullscreen:true,text:"拼命加载中~"});
    },
}
export default {
    Req
}
